package java6681.random;

import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

 class SelectNumber6681{
    private int max;
    private int min;

    public void setMax(int max) {
        if (max > min){
            this.max = max;
        }else {
            this.max = min + 1;
        }
    }
    public int getMax() {
        return max;
    }
    public void setMin(int min) {
        if (min >= 0){
            this.min = min;
        }else {
            this.min = 0;
        }
    }
    public int getMin() {
        return min;
    }
    public SelectNumber6681(){
        min = 0;
        max = 50;
    }
    public SelectNumber6681(int min,int max){
        setMax(max >= min ? max : min);
        setMin(max >= min ? min : max);
    }

    /*
     * 首先new Scanner类 输入value的值
     * 输出两个随机数数组，相减大于value
     *if判断是否大于value
     * */
    public int[] getTowNums(int value){
        if (max - min < value)
            return null;
        else if (max - min == value)
            return new int[]{min,max};
        int x,y;
        Random rand = new Random();
        x = min + rand.nextInt(max - min + 1);
        y = min + rand.nextInt(max - min + 1);
        while (true){
            if (y != x && (y -x >= value || y - x <= -value )){
                break;
            }else{
                x = min + rand.nextInt(max - min + 1);
                y = min + rand.nextInt(max - min + 1);
            }
        }
        return new int[]{x,y};
    }
    public int[] getNums(int n){
        if (max - min + 1 < n){
            return null;
        }else if (max - min +1 == n){
            int[] nums = new int[n];
            nums[0] = min;
            for (int i = 1; i < n; i++)
                nums[i] = nums[i-1]+1;
            return nums;
        }
        int[] nums = new int[n];
        for (int i = 0; i < n; i++)
            nums[i] = -1;

        Random rand = new Random();
        int x;
        for (int i = 0; i < n; i++) {
            while (!isSelect(nums,i,x = min + rand.nextInt(max - min + 1)))
               ; nums[i] = x;
        }
        Arrays.sort(nums);
        return nums;
    }

    private boolean isSelect(int[] a,int k,int x){
        for (int i = 0; i < k; i++) {
            if (x == a[i]){
                return false;
            }
        }
        return true;
    }
}

public class Menu6681 {
    static SelectNumber6681 sele = new SelectNumber6681();
    static Scanner input = new Scanner(System.in);
    public static void main(String[] args) {
        int select = 0,n,value;
        while(true){
            System.out.println("==============212006681:张舒婷=============");
            System.out.println(" 1------------- 修改随机数区间");
            System.out.println(" 2------------- 在["+ sele.getMin()+","+sele.getMax() +"]产生一组随机数");
            System.out.println(" 3------------- 在["+ sele.getMin()+","+sele.getMax() +"]生成两个随机数");
            System.out.println(" 0------------- 结束");
            System.out.println("===========================================");
            System.out.print("请输入您的选择：");
            select = input.nextInt();
            switch (select){
                case 1:
                    change();
                    break;
                case 2:
                    numbers();
                    break;
                case 3:
                    twoNumbers();
                    break;
                case 0:
                    return;
                default:
                    System.out.println("选择错误,请按任意键重新开始选择...");
                    break;
            }
        }

    }
    //修改随机数区间
    private static void change(){
        int min = 0,max = 0;
        String temp =" ";
        while(max <= min){
            System.out.println(temp + "请输入生成随机数的区间：");
            System.out.println("最小:" );
            min = input.nextInt();

            System.out.println("最大:");
            max = input.nextInt();

            temp = "输入错误";
        }
        sele.setMin(min);
        sele.setMax(max);

    }
    private static void numbers(){
        int n;
        System.out.println("请输入需要的随机数个数：");
        n = input.nextInt();
        int[] numbers = sele.getNums(n);
        System.out.println(Arrays.toString(numbers));
    }

    private static void twoNumbers(){
        int value;
        System.out.println("请输入两个数之差应大于：");
        value = input.nextInt();
        System.out.println(Arrays.toString(sele.getTowNums(value)));
    }


}
